/*
 * @lc app=leetcode.cn id=859 lang=javascript
 *
 * [859] 亲密字符串
 */

// @lc code=start
/**
 * @param {string} s
 * @param {string} goal
 * @return {boolean}
 */
function isRepeat (str) {
    let res = '';
    for(let k in str) {
        if (res.indexOf(str[k]) !== -1) {
            return true;
        }
        res += str[k];
    }
    return false;
}
var buddyStrings = function(s, goal) {
  if (s.length !== goal.length) return false;
  if (s === goal) return isRepeat(s);
  let i = 0;
  let j;
  while(s[i] === goal[i]) {
      i++;
  }
  j = i+1;
  while(j < s.length && s[j] === goal[j]) {
      j++;
  }
  if (j === s.length) return false;
  if (s[i] !== goal[j] || s[j] !== goal[i]) return false;
  j++;
  while(j < s.length) {
      if (s[j] !== goal[j]) {
          return false;
      }
      j++;
  }
  return true;
};
// @lc code=end

